package com.yxh.fragment;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yxh.R;
import com.yxh.ZybApplication;
import com.yxh.activity.AboutMeClassActivity;
import com.yxh.activity.AboutMeNotificationActivity;
import com.yxh.activity.AboutMeSettingActivity;
import com.yxh.activity.AboutMeUserCenterActivity;
import com.yxh.activity.MainActivity;
import com.yxh.dto.UserDto;
import com.yxh.util.AppUtils;
import com.yxh.util.Options;
import com.yxh.view.CircleImageView;

/**
 * 
 */

public class AboutMeFragment extends BaseFragment implements OnClickListener {

	private CircleImageView userImg;
	private TextView userTv;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;
	private UserDto userInfo;
	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = ZybApplication.getContext();
		imageLoader = ImageLoader.getInstance();
		if (!imageLoader.isInited()) {
			Options.initImageLoaderConfiguration(ImageLoader.getInstance(),
					mContext);
		}
		options = Options.getUserPicOptions();
		initRegisterReceiver();
	}

	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_about_me, null);

		view.findViewById(R.id.head_layout_back).setVisibility(View.GONE);
		((TextView) view.findViewById(R.id.head_layout_tv)).setText("我的");

		userImg = (CircleImageView) view.findViewById(R.id.setting_layout1_img);
		userTv = (TextView) view.findViewById(R.id.setting_layout1_tv);

		view.findViewById(R.id.setting_layout1).setOnClickListener(this);
		view.findViewById(R.id.setting_layout2).setOnClickListener(this);
		view.findViewById(R.id.setting_layout3).setOnClickListener(this);
		view.findViewById(R.id.setting_layout4).setOnClickListener(this);

		/*** set userInfo ***/
		initUserInfo();

		return view;

	}

	private void initUserInfo() {
		userInfo = ((MainActivity) getActivity()).getcurrentUser();
		Log.e("luoming", userInfo + "");
		if (userInfo != null) {
			if (!TextUtils.isEmpty(userInfo.userPic)) {
				imageLoader.displayImage(userInfo.userPic, userImg, options);
			}

			if (!TextUtils.isEmpty(userInfo.nickname))
				userTv.setText(userInfo.nickname);

		}
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch (id) {
		case R.id.setting_layout1:
			goUserCenter();
			break;
		case R.id.setting_layout2:
			goMyClass();
			break;
		case R.id.setting_layout3:
			goMyNotification();
			break;
		case R.id.setting_layout4:
			goMySetting();
			break;
		default:
			break;
		}
	}

	private void goUserCenter() {
		Intent it = new Intent(mContext, AboutMeUserCenterActivity.class);
		startActivity(it);
	}

	private void goMyClass() {
		Intent it = new Intent(mContext, AboutMeClassActivity.class);
		it.putExtra("resource", "0");
		startActivity(it);
	}

	private void goMyNotification() {
		Intent it = new Intent(mContext, AboutMeNotificationActivity.class);
		startActivity(it);
	}

	private void goMySetting() {
		Intent it = new Intent(mContext, AboutMeSettingActivity.class);
		startActivity(it);
	}

	/**
	 * 注册广播
	 */
	public static final String COM_CHANGE_USER_INFO = "com.change.userinfo.for.aboutme";
	public static final String COM_CHANGE_USER_PIC = "com.change.userPic.for.aboutme";
	private BroadcastReceiver mainInfoReceiver;

	private void initRegisterReceiver() {
		mainInfoReceiver = new BroadcastReceiver() {
			public void onReceive(Context context, Intent i) {
				String action = i.getAction();
				if (action.equals(COM_CHANGE_USER_INFO)) {
					// 刷新Fragment
					initUserInfo();
				} else if (action.equals(COM_CHANGE_USER_PIC)) {
					if (!TextUtils.isEmpty(i.getStringExtra("userPic"))) {
						userImg.setImageBitmap(AppUtils.getLoacalBitmap(i
								.getStringExtra("userPic")));
					}
				}
			}
		};

		IntentFilter i = new IntentFilter();
		i.addAction(COM_CHANGE_USER_INFO);
		i.addAction(COM_CHANGE_USER_PIC);
		getActivity().registerReceiver(mainInfoReceiver, i);
	}

	private void unregisterReceiver() {
		getActivity().unregisterReceiver(mainInfoReceiver);
	}

	@Override
	public void onDestroy() {
		unregisterReceiver();
		super.onDestroy();
	}

}
